//
//  CommonCollectionViewCell.m
//  HXBaseProjectDemo
//
//  Created by YaSha_Tom on 2018/2/5.
//  Copyright © 2018年 亚厦装饰股份有限公司. All rights reserved.
//

#import "CommonCollectionViewCell.h"

@interface CommonCollectionViewCell()

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) QMUILabel *messageNumberLabel;
@end

@implementation CommonCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}
- (void)initUI {
    _iconImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.iconImageView];
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(28, 28));
    }];
    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = [UIFont systemFontOfSize:12];
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.textColor = [UIColor colorWithRed:0.50 green:0.50 blue:0.50 alpha:1.00];
    [self.contentView addSubview:self.nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.top.mas_equalTo(_iconImageView.mas_bottom).offset(12);
        make.height.mas_equalTo(12);
        make.width.mas_greaterThanOrEqualTo(0);
    }];
    
    _messageNumberLabel = [[QMUILabel alloc] init];
    _messageNumberLabel.textColor = UIColorWhite;
    _messageNumberLabel.textAlignment = NSTextAlignmentCenter;
    _messageNumberLabel.layer.masksToBounds = YES;
    _messageNumberLabel.layer.cornerRadius = 10;
    _messageNumberLabel.backgroundColor = UIColorRed;
    [self.contentView addSubview:self.messageNumberLabel];
    [_messageNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_iconImageView.mas_top).offset(-4);
        make.right.mas_equalTo(_iconImageView.mas_right).offset(4);
        make.size.mas_equalTo(CGSizeMake(0, 0));
    }];
}

- (void)setCellModel:(CommonApplicationModel *)cellModel {
    _cellModel = cellModel;
    _iconImageView.image = [UIImage imageNamed:_cellModel.imageName];
    _nameLabel.text = _cellModel.name;
    if (_cellModel.unreadCount > 99) {
        _messageNumberLabel.text = @"99+";
        _messageNumberLabel.font = [UIFont systemFontOfSize:8];
    } else {
        _messageNumberLabel.text = [NSString stringWithFormat:@"%zd", _cellModel.unreadCount];
        _messageNumberLabel.font = [UIFont systemFontOfSize:10];
    }
    [_messageNumberLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_iconImageView.mas_top).offset(-10);
        make.right.mas_equalTo(_iconImageView.mas_right).offset(10);
        make.size.mas_equalTo(CGSizeMake(_cellModel.unreadCount == 0 ? 0 : 20, _cellModel.unreadCount == 0 ? 0 : 20));
    }];
}

@end
